package com.zmn.uac.client;

import com.alibaba.fastjson.JSON;
import com.zmn.common.dto2.ResponseDTO;
import com.zmn.uac.UacException;
import com.zmn.uuc.common.constant.DubboConsts;
import com.zmn.uuc.common.dio.pass.UserMobileCaptchaLoginDIO;
import com.zmn.uuc.common.dio.pass.UserMobileLoginDIO;
import com.zmn.uuc.dubbo.interfaces.pass.PassLoginRemoteService;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.stereotype.Component;

/**
 * @title: 通行证登录Dubbo接口
 * @author:Rananquan
 * @Date:2022/6/9 14:32
 */
@Slf4j
@Component
public class PassLoginClient {

    @DubboReference(version = DubboConsts.INTERFACE_VERSION, check = false)
    PassLoginRemoteService passLoginRemoteService;

    /**
     * 通过手机号验证码登录
     * @param mobileCaptchaLoginDIO
     * @return token，有效期为7天
     */
    public String loginByMobileCaptcha(UserMobileCaptchaLoginDIO mobileCaptchaLoginDIO){
        ResponseDTO<String> responseDTO = this.loginByMobileCaptchaWithResponse(mobileCaptchaLoginDIO);

        return responseDTO.getData();
    }


    /**
     * 通过手机号验证码登录
     * @param mobileCaptchaLoginDIO
     * @return token，有效期为7天
     */
    public ResponseDTO<String> loginByMobileCaptchaWithResponse(UserMobileCaptchaLoginDIO mobileCaptchaLoginDIO){
        log.info("通过手机号验证码登录入参：{}",JSON.toJSONString(mobileCaptchaLoginDIO));
        ResponseDTO<String> tokenResponse = passLoginRemoteService.loginByMobileCaptcha(mobileCaptchaLoginDIO);
        if(!tokenResponse.isSuccess()){
            log.error("通过手机号验证码登录请求失败，失败原因：{}",tokenResponse.getMessage());
            return ResponseDTO.fail(tokenResponse.getMessage());
        }

        String token = tokenResponse.getData();
        log.info("通过手机号验证码登录返回response：{}",token);

        return tokenResponse;
    }


    /**
     * 用户登出
     * @param token 登录token
     * @return 是否成功，true成功，false失败
     */
    public Boolean logout(String token){
        log.info("用户登出入参：{}",token);
        ResponseDTO<Boolean> logOutResponse = passLoginRemoteService.logout(token);
        if(!logOutResponse.isSuccess()){
            log.error("用户登出请求失败，失败原因：{}",logOutResponse.getMessage());
            throw new UacException(logOutResponse.getMessage());
        }

        Boolean logOutData = logOutResponse.getData();
        log.info("用户登出返回response：{}",logOutData);

        return logOutData;
    }


    /**
     * 通过手机号登录（无短信验证码）
     * @param userMobileLoginDIO
     * @return 三方账号token，有效期为7天
     */
    public String loginThirdByMobile(UserMobileLoginDIO userMobileLoginDIO){
        log.info("通过手机号登录入参：[{}]",userMobileLoginDIO);
        ResponseDTO<String> passLoginResponse = passLoginRemoteService.loginThirdByMobile(userMobileLoginDIO);
        if(!passLoginResponse.isSuccess()){
            log.error("通过手机号登录请求失败，失败原因：{}",passLoginResponse.getMessage());
            throw new UacException(passLoginResponse.getMessage());
        }

        String token = passLoginResponse.getData();
        log.info("通过手机号登录返回response：{}",token);

        return token;
    }
}
